﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

using SharedGamesClasses;

namespace GuiGames {

    /// <summary>
    /// Provides a GUI for the Twenty-One game in the SharedGamesClasses.
    /// </summary>
    public partial class TwentyOneForm : Form {

        Games MainGame;
        /// <summary>
        /// Constructor 
        /// </summary>
        /// <param name="form"></param>
        public TwentyOneForm(Games _game)
        {
            InitializeComponent();
            Trace.Listeners.Add(new ListBoxTraceListener(twentyOneListBox));
            this.MainGame = _game;
            InitForm();
        }

        private void InitForm()
        {
            btnHit.Enabled = false;
            btnStand.Enabled = false;
            lblDealerWon.Text = "0";
            lblNumOfAces.Text = "0";
            lblPlayerWon.Text = "0";
            lblDealerPoint.Text = "0";
            lblPlayerPoint.Text = "0";
            lblDealerPoint.Enabled = true;
            lblPlayerPoint.Enabled = true;
            lblDealerBusted.Visible = false;
            lblPlayerBusted.Visible = false;
            TwentyOne.ResetGame();
            TwentyOne.numOfGamesWon[TwentyOne.USER] = 0;
            TwentyOne.numOfGamesWon[TwentyOne.DEALER] = 0;
        }

        private void Reset()
        {
            btnDeal.Enabled = true;
            btnHit.Enabled = false;
            btnStand.Enabled = false;
            TwentyOne.ResetGame();
        }

        private void ShowPoint()
        {
            lblDealerPoint.Text = TwentyOne.GetPointsTotal(TwentyOne.DEALER).ToString();
            lblDealerWon.Text = TwentyOne.GetNumOfGamesWon(TwentyOne.DEALER).ToString();
            lblPlayerPoint.Text = TwentyOne.GetPointsTotal(TwentyOne.USER).ToString();
            lblPlayerWon.Text = TwentyOne.GetNumOfGamesWon(TwentyOne.USER).ToString();
        }

        private void ShowResult(int who)
        {
            if (who == TwentyOne.USER)
                lblPlayerBusted.Visible = true;
            else if (who == TwentyOne.DEALER)
                lblDealerPoint.Visible = true;
            else
            {
                lblPlayerBusted.Visible = false;
                lblDealerBusted.Visible = false;
            }

            lblDealerPoint.Text = TwentyOne.GetPointsTotal(TwentyOne.DEALER).ToString();
            lblDealerWon.Text = TwentyOne.GetNumOfGamesWon(TwentyOne.DEALER).ToString();
            lblPlayerPoint.Text = TwentyOne.GetPointsTotal(TwentyOne.USER).ToString();
            lblPlayerWon.Text = TwentyOne.GetNumOfGamesWon(TwentyOne.USER).ToString();
        }

        private void ShowCard()
        {
            DisplayGuiHand(TwentyOne.hands[TwentyOne.USER], tlpPlayerCards);
            DisplayGuiHand(TwentyOne.hands[TwentyOne.DEALER], tlpDealerCards);

        }

        protected override void OnClosed(EventArgs e)
        {
            MainGame.Show();
        }

        private void btnCancelGame_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnStand_Click(object sender, EventArgs e)
        {
            ShowPoint();
            switch (TwentyOne.Stand())
            {
                case -1:
                    ShowCard();
                    ShowResult(TwentyOne.DEALER);
                    Reset();
                    break;
                case 0:
                    ShowCard();
                    ShowResult(-1);
                    Reset();
                    break;
                case 1:
                    ShowCard();
                    ShowResult(TwentyOne.USER);
                    Reset();
                    break;
            }
            ShowPoint();
        }

        private void btnHit_Click(object sender, EventArgs e)
        {
            ShowPoint();
            if (TwentyOne.Hit())
            {
                ShowCard();
            }
            else
            {
                ShowCard();
                DialogResult dialogResult = MessageBox.Show("Count Ace as 1?", // The question.
                                            "You got an Ace", // The MessageBox's caption.
                                             MessageBoxButtons.YesNo,
                                             MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    TwentyOne.IncreaseNumOfUserAcesWithValueOne();
                }                
            }
            ShowPoint();
            if (TwentyOne.isBustedPlayer())
            {
                ShowCard();
                ShowResult(TwentyOne.USER);
                Reset();
            }
            else
            {
                ShowCard();
            }
            ShowPoint();
        }

        private void btnDeal_Click(object sender, EventArgs e)
        {
            TwentyOne.ResetGame();
            lblDealerBusted.Visible = false;
            lblPlayerBusted.Visible = false;
            ShowPoint();
            switch (TwentyOne.Deal())
            {
                case -1:
                    ShowCard();
                    ShowResult(-1);
                    break;
                case 0:
                    ShowCard();
                    DialogResult dialogResult = MessageBox.Show("Count Ace as 1?", // The question.
                                            "You got an Ace", // The MessageBox's caption.
                                             MessageBoxButtons.YesNo, 
                                             MessageBoxIcon.Question);
                        if (dialogResult == DialogResult.Yes) {
                            TwentyOne.IncreaseNumOfUserAcesWithValueOne();
                        }
                    btnHit.Enabled = true;
                    btnStand.Enabled = true;
                    btnDeal.Enabled = false; 
                    break;
                case 1:
                    ShowCard();
                    btnHit.Enabled = true;
                    btnStand.Enabled = true;
                    btnDeal.Enabled = false;                    
                    break;
                default:
                    break;
            }
            ShowPoint();
        }

        private void DisplayGuiHand(Hand hand, TableLayoutPanel tableLayoutPanel)
        {

            tableLayoutPanel.Controls.Clear();  // Remove any cards already being shown.

            foreach (Card card in hand)
            {

                // Construct a PictureBox object.
                PictureBox pictureBox = new PictureBox();
                // Tell the PictureBox to use all the space inside its square.
                pictureBox.Dock = DockStyle.Fill;
                // Remove spacing around the PictureBox. (Default is 3 pixels.)
                pictureBox.Margin = new Padding(0);

                pictureBox.Image = Images.GetCardImage(card);

                // Add the PictureBox object to the tableLayoutPanel.
                tableLayoutPanel.Controls.Add(pictureBox);
            }
        }

        private void TwentyOneForm_Load(object sender, EventArgs e)
        {

        }


    } //end class TwentyOneForm
} //end namespace
